package com.z.controller;

import com.z.pojo.Users;
import com.z.service.UsersService;
import com.z.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUServlet")
public class AddUServlet extends HttpServlet {
    private final UsersService usersService = new UsersServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf-8");

        String account = req.getParameter("account");
        String uname = req.getParameter("uname");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String gender = req.getParameter("gender");

        Users users = new Users();

        users.setUname(uname);
        users.setPhone(phone);
        users.setPassword(password);
        users.setGender(gender);
        users.setAccount(account);
        users.setLevel(1);

        System.out.println(users);

        Integer integer = usersService.addUsers(users);
        System.out.println(integer);
        if(integer > 0)
        {
            req.setAttribute("msg","添加成功");
        }else
        {
            req.setAttribute("msg","添加失败");
        }
        req.getRequestDispatcher("/manageUser").forward(req,resp);
    }
}
